package com.cart.seon.usbconnection.home;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.cart.seon.usbconnection.R;
import com.cart.seon.usbconnection.base.service.MusicPlayerService;

public class MediaPlayerActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getName();

    private final MusicPlayerService musicPlayerService = MusicPlayerService.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_media_player);

        handlePermission();

        findViewById(R.id.play).setOnClickListener(v -> {
            String externalPath = Environment.getExternalStorageDirectory().getPath();
            musicPlayerService.playByPath(externalPath + "/Test/alone.mp3");
        });

        findViewById(R.id.pause).setOnClickListener(v -> musicPlayerService.pause());

        findViewById(R.id.stop).setOnClickListener(v -> musicPlayerService.stop());
    }


    /**
     * 检查app是否拥有存储权限，如果没有的话，提醒用户开启权限
     */
    public void handlePermission() {
        // 检查是否开启 Manifest.permission.xxx
        // (xxx 为权限，根据自己需求添加）
        if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Permission has been allowed", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "ask for permission", Toast.LENGTH_SHORT).show();
            // 请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            Log.d(TAG, "handlePermission: has asked");
        }
    }

}